﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace CrazyClient
{
    /// <summary>
    /// Interaction logic for FadeIn.xaml
    /// </summary>
    public partial class FadeOut : UserControl,ITransition
    {
        public FadeOut()
        {
            InitializeComponent();
            To = 0;
            Duration = new Duration(new TimeSpan(0, 0, 1));
        }
        public void Go()
        {
            if (ParentPanel is LyricPanel)
            {
                LyricPanel p = ParentPanel as LyricPanel;
                DoubleAnimation anim = new DoubleAnimation(To, Duration);
                anim.Completed += new EventHandler(anim_Completed);
                p.TheContent.BeginAnimation(VisualBrush.OpacityProperty,anim,HandoffBehavior.Compose);
                
            }
        }

        void anim_Completed(object sender, EventArgs e)
        {
            LyricPanel p = ParentPanel as LyricPanel;
            p.TheContent.BeginAnimation(VisualBrush.OpacityProperty, null, HandoffBehavior.Compose);
            
        }


        public void Finish()
        {
            ////      LyricPanel p = ParentPanel as LyricPanel;
            //     p.Transition.Children.Remove(trans);
            //trans = null;
        }

        public bool IsDone
        {
            get
            {
                return _isDone;
            }
        }
        bool _isDone;


        #region ParentPanelProperty
        public static readonly DependencyProperty ParentPanelProperty =
            DependencyProperty.Register("ParentPanel", typeof(LyricPanel),
            typeof(FadeOut),
            new PropertyMetadata(OnParentPanelChanged));

        static private void OnParentPanelChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            //FadeIn panel = obj as FadeIn;
            //obj.Go()


            //     TranslateTransform3D d = new TranslateTransform3D(0, 0, 0);
            ////     ((PerspectiveCamera)obj).Transform = d;
            //    d.BeginAnimation(TranslateTransform3D.OffsetXProperty, new DoubleAnimation(0, 1, new Duration(new TimeSpan(0, 0, 5))));
            //Debug.WriteLine("Mass property changed from {0} to {1}", e.OldValue, e.NewValue);
        }

        public LyricPanel ParentPanel
        {
            get
            {
                object result = GetValue(ParentPanelProperty);
                return (LyricPanel)result;
            }
            set
            {
               
                SetValue(ParentPanelProperty, value);
            }
        }

        #endregion

        #region ToProperty
        public static readonly DependencyProperty ToProperty =
            DependencyProperty.Register("To", typeof(double),
            typeof(FadeOut),
            new PropertyMetadata(OnToChanged));

        static private void OnToChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            //FadeIn panel = obj as FadeIn;
            //obj.Go()


            //     TranslateTransform3D d = new TranslateTransform3D(0, 0, 0);
            ////     ((PerspectiveCamera)obj).Transform = d;
            //    d.BeginAnimation(TranslateTransform3D.OffsetXProperty, new DoubleAnimation(0, 1, new Duration(new TimeSpan(0, 0, 5))));
            //Debug.WriteLine("Mass property changed from {0} to {1}", e.OldValue, e.NewValue);
        }

        public double To
        {
            get
            {
                object result = GetValue(ToProperty);
                return (double)result;
            }
            set
            {

                SetValue(ToProperty, value);
            }
        }

        #endregion

        #region DurationProperty
        public static readonly DependencyProperty DurationProperty =
            DependencyProperty.Register("Duration", typeof(Duration),
            typeof(FadeOut),
            new PropertyMetadata(OnDurationChanged));

        static private void OnDurationChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            //FadeIn panel = obj as FadeIn;
            //obj.Go()


            //     TranslateTransform3D d = new TranslateTransform3D(0, 0, 0);
            ////     ((PerspectiveCamera)obj).Transform = d;
            //    d.BeginAnimation(TranslateTransform3D.OffsetXProperty, new DoubleAnimation(0, 1, new Duration(new TimeSpan(0, 0, 5))));
            //Debug.WriteLine("Mass property changed from {0} to {1}", e.OldValue, e.NewValue);
        }

        public Duration Duration
        {
            get
            {
                object result = GetValue(DurationProperty);
                return (Duration)result;
            }
            set
            {

                SetValue(DurationProperty, value);
            }
        }

        #endregion
    }
}
